package com.hdoit.kylin.web.user.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.group.service.IGroupService;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.TeacherStudent;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.user.service.ITeacherStudentService;
import com.hdoit.kylin.logical.viewmodel.user.StudentSearch;
import com.hdoit.kylin.logical.viewmodel.user.TeacherStudentSearch;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.SexStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

@Controller
@RequestMapping(value="/admin/user/student/")
public class StudentController extends BaseController{
	@Override
	public String getRoutePath() {
		return "areas/admin/user/student/";
	}
	
	private IStudentService studentService;
	private ITeacherService teacherService;
	private IGroupService groupService;
	private IEmployeeService employeeService;

	public IStudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	public ITeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public IGroupService getGroupService() {
		return groupService;
	}

	@Resource
	public void setGroupService(IGroupService groupService) {
		this.groupService = groupService;
	}

	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	private final String indexViewName="index";
	private final String outputViewName="output";
	private final String studentDetailedViewName="studentDetailed";
	private final String saveViewName="save";
	private final String deleteViewName="delete";
	private final String checkAccountViewName="checkAccount";
	
	@RequestMapping(value=indexViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String index(Model model,StudentSearch studentSearch,PagerModel pagerModel,boolean ajaxPost){
		String groupId=teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId());
		if(null==groupId){
			groupId=studentService.queryGroupIdByLoginId(getLoginModel().getLoginId());
		}
		
		List<Student> students;
		if(null==groupId){
			pagerModel.setTotalAmount(studentService.queryCount(studentSearch));
			students=studentService.queryList(studentSearch,pagerModel);
		}
		else{
			pagerModel.setTotalAmount(studentService.queryCountByGroupId(studentSearch, groupId));
			students=studentService.queryListByGroupId(studentSearch,pagerModel,groupId);
		}
		
		model.addAttribute("students",students);
		model.addAttribute("studentSearch",studentSearch);
		model.addAttribute(ManageContainer.PAGERKEY,pagerModel);
		
		if(ajaxPost){
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value=studentDetailedViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String studentDetailed(Model model,String key){
		Student student;
		if(StringUtils.isEmpty(key)){
			student=new Student();
			student.setSex(SexStatus.male);
		}
		else{
			student=studentService.queryModel(key);
		}
		
		model.addAttribute("student", student);
		
		return view(studentDetailedViewName);
	}
	
	@RequestMapping(value=saveViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String save(Model model,Student student){
		if(StringUtils.isEmpty(student.getEmployeeId())){
			student.setEmployeeId(CommonUtils.getKeyId());
			student.setEmployeeName(student.getStudentName());
			student.setCreateDate(DateUtils.getCurrent());
			student.setCreateUserId(getLoginModel().getLoginId());
			student.setCreateUserName(getLoginModel().getLoginName());
			
			String groupId=teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId());
			if(null==groupId){
				groupId=studentService.queryGroupIdByLoginId(getLoginModel().getLoginId());
			}
			if(!StringUtils.isEmpty(groupId)){
				student.setGroupId(groupId);
			}
			else{
				student.setGroupId("noGroup");
			}
			
        	student.setModelStatus(ModelStatus.add);
		}
		else{
			student.setEmployeeName(student.getEmployeeAccount());
			student.setModifyDate(DateUtils.getCurrent());
			student.setModifyUserId(getLoginModel().getLoginId());
			student.setModifyUserName(getLoginModel().getLoginName());
			student.setModelStatus(ModelStatus.update);
		}
		student.setEmployeeCode(ChineseUtils.getPingYin(student.getStudentName()));
		student.setMarkCode(ChineseUtils.getPinYinHeadChar(student.getStudentName()));
		
		ActionResult actionResult;
		if(studentService.applyChange(student)){
			actionResult=new ActionResult();
		}
		else{
			actionResult=new ErrorResult();
		}
		
		model.addAttribute("student", student);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		
		return view(studentDetailedViewName);
	}
	
	@RequestMapping(value=deleteViewName,method={RequestMethod.GET,RequestMethod.POST},produces = "text/json;charset=UTF-8")
	public @ResponseBody String delete(String key){
		if(studentService.deleteModel(key)&&employeeService.deleteModel(key)){
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
	
	@RequestMapping(value = checkAccountViewName, method = { RequestMethod.GET })
	public @ResponseBody String checkAccount(String employeeId,String employeeAccount){
		return String.valueOf(!employeeService.checkAccount(employeeId, employeeAccount));
	}
	
	@RequestMapping(value = "graduate",method={RequestMethod.GET,RequestMethod.POST},produces = "text/json;charset=UTF-8")
	public @ResponseBody String graduate(String key){
		System.err.println(key);
		Student student = studentService.queryModel(key);
		student.setGraduateMark(YesOrNoInteger.yes);
		student.setGraduationDate(DateUtils.getCurrent());
		student.setModelStatus(ModelStatus.update);
		if(studentService.applyChange(student)){
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}
